/**
 * Created by rockyren on 14/12/2.
 */
var commonServices = angular.module('commonServices',[]);

/**
 * 创建CRUD方法对象
 * @param nestedSource string: 请求的模块名,如'cart'
 * @param $http
 * @param selectedActionNames array: 选择的方法名,如['get','show']
 * @returns {*}
 */
function crudNestedFactory(nestedSource,$http,selectedActionNames) {
  function getRequestBaseUrl() {
    return [
      'api',
      nestedSource
    ].join('/');
  }

  var actions = {
    get: function(){
      return $http.get(getRequestBaseUrl());
    },
    show: function(id) {
      return $http.get(getRequestBaseUrl() + '/' + id);
    },
    store: function(postData) {
      return $http.post(getRequestBaseUrl(),postData);
    },
    update: function(id, postData) {
      return $http.put(getRequestBaseUrl() + '/' + id,postData);
    },
    destroy: function(id) {
      return $http.delete(getRequestBaseUrl() + '/' + id);
    }
  };

  function getActionsObj(actions,selectedActionNames) {
    var selectedActions = {};
    for(var i=0;i<selectedActionNames.length;i++) {
      //如果在actions存在该属性,则加入selectedActions
      if(actions.hasOwnProperty(selectedActionNames[i])){
        selectedActions[selectedActionNames[i]] = actions[selectedActionNames[i]];

      }
    }

    return selectedActions;
  }

  return getActionsObj(actions,selectedActionNames);


}

commonServices.factory('cartServices',['$http',function($http){
  return crudNestedFactory('cart',$http,['get','store','update','destroy']);
}]);

commonServices.factory('addressServices',['$http',function($http){
  return crudNestedFactory('personal/address',$http,['get','show','store','update','destroy']);
}]);


commonServices.factory('districtServices',['$http',function($http){
  return {
    getProvince: function(){
      return $http.get('api/district');
    },
    getCity: function(provinceId){
      return $http.get('api/district' + '/' + provinceId);
    },
    getRegion: function(provinceId,cityId){
      return $http.get('api/district' + '/' + provinceId + '/' + cityId);
    }
  }
}]);

commonServices.factory('orderServices', ['$http', function($http){
  return crudNestedFactory('order', $http, ['get', 'show', 'store', 'update', 'destroy']);
}]);

commonServices.factory('siftServices', function(){
  return {
    siftByOrderStatus: function(status, orderList){
      if(status == 0) return orderList;
      var siftOrderList = [];
      for(var i=0; i<orderList.length; i++){
        var order = orderList[i];
        if(order.status == status){
          siftOrderList.push(order);
        }

      }
      return siftOrderList;
    }
  }
});

commonServices.factory('errorShowServices', function(){
  return {
    alertErrorMessage: function(perLabel, messageObj) {
      var errorMessage = '';
      //如果message是字符串，则直接显示
      if(typeof messageObj == 'string'){
        errorMessage = messageObj;
      }else {
        for(var p in messageObj) {
          errorMessage += p + ':' + messageObj[p] + '\n'
        }
      }
      alert(perLabel + ':\n' + errorMessage);
    }
  }
});